﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading;
using A2DFramework.QueueService.QueueConfiguration;

namespace A2DFramework.QueueService
{
    public class ShareFileBroker
    {
        private FileService.FileService fileSrv;
        public ShareFileBroker(FileService.FileService fs)
        {
            this.fileSrv = fs;
        }

        public string Save(string xml)
        {
            string fileName = System.IO.Path.Combine(QueueConfiguration.QueueConfiguration.MSMQSettings.NetworkLocation, Guid.NewGuid().ToString("N"));
            fileSrv.Save(xml, fileName);
            return fileName;
        }

        public string GetContentAndDelete(string path)
        {
            string content = fileSrv.ReadToEnd(path);

            if (QueueConfiguration.QueueConfiguration.MSMQSettings.AutoDeleteNetworkFile)
                ThreadPool.QueueUserWorkItem(new WaitCallback(DeleteFile), path);

            return content;
        }
        private void DeleteFile(object state)
        {
            string path = (string)state;
            fileSrv.Delete(path);
        }
    }
}
